package com.betty.message;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.support.v4.widget.CursorAdapter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    ContentResolver resolver;
    ListView lv;
    String sms_uri="content://sms/inbox";
    Cursor cursor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        resolver = getContentResolver();
        lv=(ListView)findViewById(R.id.lv);
        cursor = resolver.query(Uri.parse(sms_uri),null,null,null,null);
        lv.setAdapter(new Myadpter(this,cursor,CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER));

    }
//    public void querymes(View view){

//    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (cursor!=null){
            cursor.close();
        }
    }

    class  Myadpter extends CursorAdapter{
        public Myadpter(Context context,Cursor c,int flag){
            super(context,c,flag);

        }

//        创建一个试图---引入ListView中要展示的子视图
        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            return getLayoutInflater().inflate(R.layout.list_item,null);
        }
//绑定数据的方法
        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            TextView tv_number=(TextView)findViewById(R.id.tv_number);
            TextView tv_body=(TextView)findViewById(R.id.tv_body);
            TextView tv_type=(TextView)findViewById(R.id.tv_type);
            String number=cursor.getString(cursor.getColumnIndex("address"));
            String body=cursor.getString(cursor.getColumnIndex("body"));
            int type=cursor.getInt(cursor.getColumnIndex("type"));
            tv_number.setText(number);
            tv_body.setText(body);
            if (type==1){
                tv_type.setText("接收");
            }else {
                tv_type.setText("发送");
            }

        }
    }
}
